//
//  MineNotifyVC.m
//  Tunnel
//
//  Created by wbb on 2022/3/8.
//

#import "MineNotifyVC.h"
#import "MineNotifyCell.h"



@interface MineNotifyVC ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView * tableView;
@property (nonatomic, strong) NSMutableArray * dataArr;
@property (nonatomic, strong) NSMutableArray * selectArr;

@end

@implementation MineNotifyVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"通知设置";
    self.dataArr = @[].mutableCopy;
    NSArray * keyArr = @[@"APP_MSG_TYPE"];
    self.selectArr = [NSMutableArray array];
    kWEAK_SELF;
    [TYNetWorkTool getRequest:TYGetMsgSet parameters:@{} successBlock:^(BOOL success, NSDictionary * _Nonnull res, NSString * _Nonnull msg) {
        if(success){
            NSArray * resArr = res[@"data"];
            if(resArr.count == 0){
                weakSelf.selectArr = @[@"ENERGYUSEEXCEPT",
                                       @"DEVICE_ALERTS",
                                       @"INSIDE_EQUIPMENT_MONITORING_ALARMING",
                                       @"EMERGENCECREATION",
                                       @"MULTI_VEHICLE_ACCIDENTS",
                                       @"RETROGRADE",@"SCATTERED_OBJECTS",
                                       @"PLC_FAULT_ALARMS",
                                       @"THRESHOLD_MONITORING_ALARMS",
                                       @"SPARESLOW",@"WORKORDERDCL",
                                       @"THE_DEVICE_IS_OFFLINE",
                                       @"SECURITYREVIEW",
                                       @"WORKORDERASSIGN",
                                       @"FOCUS_ON_VEHICLES",
                                       @"MANUALLY_FILL_IN_THE_ALARM"].mutableCopy;
            }else{
                for (NSDictionary * dic in resArr) {
                    [weakSelf.selectArr addObject:dic[@"msgType"]];
                }
            }
        }
        
        [TYNetWorkTool postRequest:TYGetDictQuery parameters:@{
            @"types":keyArr
        } successBlock:^(BOOL success, id  _Nonnull data, NSString * _Nonnull msg) {
            if(success){
                for (NSDictionary * dict in  data[@"data"]) {
                    MineNotifyModel * model = [MineNotifyModel new];
                    model.title = dict[@"dictLabel"];
                    model.dictValue = dict[@"dictValue"];
                    model.isSelect = false;
                    for (NSString * key in self.selectArr) {
                        if([key isEqualToString:model.dictValue]){
                            model.isSelect = true;
                            break;
                        }
                    }
                    [weakSelf.dataArr addObject:model];
                    [weakSelf.tableView reloadData];
                }
                
            }

        } failureBlock:^(NSString * _Nonnull description) {

        }];
    } failureBlock:^(NSString * _Nonnull description) {
        
    }];

    [self initUI];

    
}

- (void)initUI {
    
    self.view.backgroundColor = hexColor(F6F7F9);

    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(12);
        make.right.offset(-12);
        make.bottom.top.offset(0);
    }];
    self.tableView.contentInset = UIEdgeInsetsMake(12, 0, 0, 0);

}

#pragma mark - UITableViewDelegate, UITableViewDataSource ----
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArr.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    return 44;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
   static NSString *cellIdentifier = @"MineNotifyCell";
    MineNotifyCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell) {
        cell = [[MineNotifyCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }

    // 获取总行数
    NSInteger totalRows = [tableView numberOfRowsInSection:indexPath.section];

    // 设置圆角
    [cell setCornerForIndexPath:indexPath totalRows:totalRows dataSource:self.dataArr];

    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    MineNotifyModel * model =  self.dataArr[indexPath.row];
    model.isSelect = !model.isSelect;
    NSMutableSet * mset = [NSMutableSet setWithArray:self.selectArr];
    if(!model.isSelect){
        [mset removeObject:model.dictValue];
    }else{
        [mset addObject:model.dictValue];
    }
    self.selectArr = [mset allObjects].mutableCopy;
    [TYNetWorkTool postRequest:TYSetMsg parameters:@{@"msgTypes":self.selectArr} successBlock:^(BOOL success, id  _Nonnull data, NSString * _Nonnull msg) {
        [tableView reloadData];
    } failureBlock:^(NSString * _Nonnull description) {
        
    }];
    
}

#pragma mark ---- lazy ----
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _tableView;
}
@end


